<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title>绑定微信</title>
	<link rel="stylesheet" type="text/css" href="../../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../../css/style.css" />
	<link rel="stylesheet" type="text/css" href="../../css/yzl.css" />
	<link rel="stylesheet" href="../../css/sqc.css">
</head>

<body style="background-color: #fff;height: 100vh">
	<div style="height: 80px;"></div>
	<div id="blank" class="header">
		<div v-myclick="logout" style="position: absolute; right:5%;top:30%; font-size: 14px;">退出账号</div>
	</div>
	<div id="app" style="padding: 0 5%;">
		<div class="select-title-text">绑定微信号</div>
		<div class="select-title" style="margin-top: 10px">你赚取的收入我们将会打入该微信账户</div>
		<!-- <button class="true-btn" style="background-color: #009933; color: #fff" v-show="logintype == 1" v-myclick="login_wx">打开微信</button> -->
		<button class="true-btn" style="background-color: #009933; color: #fff" v-show="logintype == 2"
			v-myclick="['wx', auth_wx]">打开微信</button>
	</div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/app.js"></script>
<script src="../../script/common.js"></script>
<script src="../../script/url.js"></script>
<script src="../../script/components.js"></script>
<script type="text/javascript" src="../../script/sha1.min.js"></script>
<script type="text/javascript" src="../../script/jquery.js"></script>

<script>
	var vm,
		wx,
		aliTradePlus,
		talkingData,
		bindInfos,
		logintype,
		WXopenid;
	apiready = function () {
		new Vue({
			el: '#blank',
			methods: {
				logout: function () {
					api.confirm({
						title: '提示',
						msg: '确定退出当前账号吗',
						buttons: ['确定', '取消']
					}, function (ret) {
						if (ret.buttonIndex == 1) {
							closeWin('settings');
							closeWin('index');
							removePrefs('token');
							removePrefs('imtoken');
							removePrefs('portrait');
							removePrefs('username');
							removePrefs('portrait');
							removePrefs('member_id');
							removePrefs('member_im_id');
							api.openWin({
								reload: true,
								name: 'login',
								url: '../login/login.html',
								vScrollBarEnabled: false,
								slidBackEnabled: false
							});
							// openWin('../login/login', {from: api.winName});
						}
					});
				}
			}
		})
		logintype = api.pageParam.logintype;
		WXopenid = api.pageParam.wx_openid;
		console.log(WXopenid)
		wx = api.require('wx');
		initApp();
		doubleBack();
		api.parseTapmode();
		addListeners();
	};
	function initApp() {
		vm = new Vue({
			el: '#app',
			data: {
				phone: '',
				password: '',
				code: '',
				appInstalled: {
					wx: true,
				},
				bindInfos: {
					wx: null,
				},
				active: false,
				loginMode: 1,
				warn: false,
				messagePass: false,
				warnText: null,
				logintype: logintype
			},

			computed: {
				ifAuth: function () {
					return {
						wx: this.bindInfos.wx == '1'
					}
				},
			},
			methods: {
				login_init: function (user, provider) {
					closeWin('index');
					setPrefs('user', user);
					setPrefs('token', user.token);
					setPrefs('imtoken', user.member_im_token);
					setPrefs('username', user.nickname);
					setPrefs('portrait', user.portrait);
					setPrefs('member_id', user.member_id);
					setPrefs('member_im_id', user.member_im_id);
					setPrefs('level_id', user.level_id);

					// setRongtoken();
					api.sendEvent({
						name: 'reLogin'
					});
					// openWin('../../index');
					api.openWin({
						reload: true,
						name: 'index',
						url: '../../index.html',
						hScrollBarEnabled: false,
						slidBackEnabled: false,
					});
				},
				bindAccount: function (params, data, provider) {
					data['jpush_token'] = getPrefs('jpush_token') || '';
					if (hasPrefs('member_id')) {
						data.member_id = getPrefs('member_id');
					}
					var callback = function (ret) {
						console.log(JSON.stringify(ret))
						if (ret.content.result) {
							// 已完善
							vm.login_init(ret.content, provider);
						} else {
							// 未完善
							vm.login_init(ret.content, provider);
						}
					};
					var errback = function (err) {
						api.toast({
							msg: '该微信已绑定其他账号',
							duration: 3000,
							location: 'middle'
						});
					}
					pinet.post(URL.oauthLogin, data, callback, errback);
				},
				// 微信登录
				login_wx: function () {
					wx.auth({}, function (ret, err) {
						if (!ret.status) {

						} else {
							wx.getToken({
								code: ret.code
							}, function (ret, err) {
								if (!ret.status) {
									// alert('获取授权 accessToken失败:' + err.code);
									api.toast({
										msg: '微信开小差了，请关闭app重新授权',
										duration: 3000,
										location: 'middle'
									});
									return false
								} else {
									showProgress();
									wx.getUserInfo({
										accessToken: ret.accessToken,
										openId: ret.openId
									}, function (ret, err) {
										if (!ret.status) {
											// alert('获取用户信息失败:' + err.code);
											return false;
										} else {
											var params = {
												nickname: ret.nickname,
												sex: ret.sex,
												portrait: ret.headimgurl,
												openid: ret.openid,
												unionid: ret.unionid || '',
												oauth: 'Weixin',
												headimgurl: ret.headimgurl,
												nickname: ret.nickname
											};
											var data = {
												openid: ret.openid,
												oauth: 'Weixin',
												unionid: ret.unionid || ''
											};
											vm.bindAccount(params, data, 'Weixin');
										}
									});
								}
							});
						}
					});
				},
				login: function () {
					showProgress();
					var data = {
						oauth: this.pageParam.oauth,
						openid: this.pageParam.openid,
						unionid: this.pageParam.unionid ? this.pageParam.unionid : '',
						nickname: this.pageParam.nickname,
						sex: this.pageParam.sex,
						portrait: this.pageParam.portrait,
						phone: this.phone,
						verify_code: this.verify_code,
						invite_code: this.invite_code,
						jpush_token: getPrefs('jpush_token') || ''
					};
					if (hasPrefs('member_id')) {
						data.member_id = getPrefs('member_id');
					}
					var callback = function (ret) {
						vm.login_init(ret.content);
					};
					var errback = function (err) {
						api.toast({
							msg: '该微信已绑定其他账号',
							duration: 3000,
							location: 'middle'
						});
					}
					pinet.post(URL.oauthRegister, data, callback, errback);
				},
				// 微信授权
				auth_wx: function () {
					wx.auth({}, function (ret) {
						if (!ret.status) {
						} else {
							wx.getToken({
								code: ret.code
							}, function (ret, err) {
								if (!ret.status) {
									// alert('获取授权 accessToken失败:' + err.code);
									api.toast({
										msg: '微信开小差了，请关闭app重新授权',
										duration: 3000,
										location: 'middle'
									});
									return false;
								} else {
									wx.getUserInfo({
										accessToken: ret.accessToken,
										openId: ret.openId
									}, function (ret, err) {
										if (!ret.status) {
											// alert('获取用户信息失败:' + err.code);
											return false;
										} else {
											var usermessage = ret
											console.log(JSON.stringify(usermessage))
											var data = {
												openid: ret.openid,
												member_id: getPrefs('member_id'),
												oauth: 'Weixin',
												unionid: ret.unionid || ''
											};
											var callback = function () {
												vm.bindInfos.wx = '1';
												var user = JSON.parse(getPrefs('user'));
												user.wx_openid = ret.openid
												setPrefs('user', user);
												alert('绑定成功');
												console.log(WXopenid)
												if(WXopenid === null){
													vm.updUser(ret.headimgurl, ret.nickname);
													return false;
												}
												openWin('wxwin', { headimgurl: ret.headimgurl, nickname: ret.nickname })
												// openWin('../../index');
											};
											var errback = function () {
												api.toast({
													msg: '该微信已绑定其它账户',
													duration: 3000,
													location: 'middle'
												});
											}
											pinet.post(URL.oauthBind, data, callback, errback);
										}
									});
								}
							});
						}
					});
				},
				updUser: function (headimgurl, nickname) {
					var headimgurl = headimgurl;
					var nickname = nickname;
					var data = {
						member_id: getPrefs('member_id'),
						nickname: nickname,
						portrait: headimgurl,
					};
					var callback = function (ret, err) {
						var user = JSON.parse(getPrefs('user'));
						user.nickname = nickname;
						user.portrait = headimgurl;
						setPrefs('user', user);
						api.openWin({
							reload: true,
							name: 'index',
							url: '../../index.html',
							hScrollBarEnabled: false,
							slidBackEnabled: false,
						});
					};
					var errback = function (ret) {
						// console.log(JSON.stringify(ret));
						api.toast({
							msg: ret.message,
							duration: 3000,
							location: 'middle'
						});
						api.openWin({
							reload: true,
							name: 'index',
							url: '../../index.html',
							hScrollBarEnabled: false,
							slidBackEnabled: false,
						});
					};
					pinet.post(URL.updUserInfo, data, callback, errback);
				}
			}
		})
	}

	function addListeners() {

	}
</script>

</html>